{include file="common/header" /}

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body" >
                    <form class="layui-form" action="{$action}" method="post" id="add">
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-col-12">
                                <label class="layui-form-label">标题</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" name="title" value="{$info.title}" style="width: 700px;font-family:Arial;" id="title">
                                </div>
                            </div>

                            <div class="layui-col-12">
                                <label class="layui-form-label">简介</label>
                                <div class="layui-input-block" style="width: 80%;">
                                    <textarea name="brief" class="layui-input" rows="4" style="height: 92px; margin: 0px 1.35px 0px 0px; width: 1006px;">{$info.brief}</textarea>
                                </div>
                            </div>

                            <div class="layui-col-12">
                                    <label class="layui-form-label">内容</label>
                                    <div class="layui-input-block" style="width: 80%;">
                                        <textarea id="container" name="content" lay-verify="content">{$info.content}</textarea>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="layui-col-12">
                                <label class="layui-form-label">首图</label>
                                <div class="layui-upload layui-input-block">
                                    <button type="button" class="layui-btn" id="test1">上传图片</button> <br>
                                    <img class="layui-upload-img" id="demo1" {if(!empty($info.file_url))} src="{$info.file_url}" {/if}>
                                    <p id="demoText"></p>
                                    <div class="layui-upload-list">
                                        <input type="hidden" name="file_path" value="{$info.file_path}" id="file_path">
                                  </div>
                                </div>
                            </div>

                            <div class="layui-col-12">
                                <label class="layui-form-label">作者</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" name="author" value="{$info.author}" style="width: 160px;">
                                </div>
                            </div>

                            <div class="layui-col-12" style="margin-top: 5px;">
                                <label class="layui-form-label">分类</label>
                                <div class="layui-input-block" style="width: 10%;">
                                    <select name="category" >
                                        <option value="">请选择</option>
                                        {if !empty($category_list)}
                                        {foreach name="category_list" item="vo"}
                                        <option value="{$key}" {if($key eq $info['category'])} selected="" {/if}>{$vo}</option>
                                        {/foreach}
                                        {/if}
                                    </select>
                                </div>
                            </div>

                            <div class="layui-col-12">
                                <label class="layui-form-label">是否发布：</label>
                                <div class="layui-input-block" >
                                    <input type="radio" name="is_publish" value="1" title="是" {if($info['is_publish'] eq 1)}checked{/if}>
                                    <input type="radio" name="is_publish" value="-1" title="否" {if($info['is_publish'] eq -1)}checked{/if}>
                                </div>
                            </div>
                        </div>
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="article-form" type="submit">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                <button type="button" class="layui-btn layui-btn-warm" onclick="javascript:history.back(-1);">返回</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

{include file="common/footer" /}
<script>

var ue = UE.getEditor('container', {
    initialFrameHeight:400,
    topOffset: 0,
    zIndex: 100
});

layui.config({
    base: '/static/admin/' //静态资源所在路径
}).use(['form', 'upload'], function(){
    var upload = layui.upload;

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        ,url: '{:url("Common/upload")}' //改成您自己的上传接口
        ,before: function(obj){
          //预读本地文件示例，不支持ie8
          obj.preview(function(index, file, result){
            $('#demo1').attr('src', result); //图片链接（base64）
          });
        }
        ,done: function(res){
          //如果上传失败
          if(res.code!=1){
            return layer.msg(res.msg);
          } else { //上传成功
            $('#file_path').val(res.data.file_path);
          }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });
});

$(function(){
    $('#add').ajaxForm({
        beforeSubmit: checkForm, 
        success: complete, 
        dataType: 'json'
    });

    function checkForm(formData, jqForm, options){
        if( '' == $.trim($('#title').val())){
            layer.msg('请输入文章标题',{icon:2,time:1500,shade: 0.1}, function(index){
                layer.close(index);
            });
            return false;
        }
        // console.log(formData);return false;
        $('.layui-btn').attr('disabled',true);
        return true;
    }

    function complete(data){
        if(data.code==0){
            layer.msg(data.msg, {icon: 6,time:1500,shade: 0.1}, function(index){
                window.location.href="{:url('article/index')}";
            });
        }else{
            layer.msg(data.msg, {icon: 5,time:1500,shade: 0.1});
            $(".layui-btn").attr('disabled',false);
            return false;   
        }
    }
});

</script>